//
//  UserCenterViewController.m
//  classroom
//
//  Created by apple on 12-10-23.
//  Copyright (c) 2012年 __MyCompanyName__. All rights reserved.
//

#import "UserCenterViewController.h"
#import "LoginViewController.h"
#import <QuartzCore/QuartzCore.h>
#import "RegisterViewController.h"
#import "CourseRecommendViewController.h"
#import "CommendCourseTableViewController.h"
#import "UserSetUpViewController.h"
#import "LoginStatus.h"
#import "Notifications.h"
#import "UserCourseViewController.h"
#import "SettingsViewController.h"


@interface UserCenterViewController ()

@end

@implementation UserCenterViewController

@synthesize label1;
@synthesize label2;
@synthesize isLogin;
@synthesize ifLogined;
@synthesize courseButton;
@synthesize recommendButton;
@synthesize settingButton;
@synthesize userImage;



- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
        self.title=@"个人中心";
        ifLogined=FALSE;
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    
    [self setUserCenterView];
    
    self.userImage.userInteractionEnabled = YES;
    UITapGestureRecognizer *recognizer = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(clickCamera)];
    [recognizer setNumberOfTapsRequired:1];
    [self.userImage addGestureRecognizer:recognizer];

    //测试用
    [LoginStatus sharedLoginStatus].ifLogined=true;
    
}

- (void)viewWillAppear:(BOOL)animated{
    [self setUserCenterView];
}

- (void)viewDidUnload
{
    [self setCourseButton:nil];
    [self setRecommendButton:nil];
    [self setSettingButton:nil];
 
    [self setLabel2:nil];
    [self setLabel1:nil];
    [userImage release];
    userImage = nil;
    [loginButton release];
    loginButton = nil;
    [registerButton release];
    registerButton = nil;
    [logoutButton release];
    logoutButton = nil;
    [super viewDidUnload];
    // Release any retained subviews of the main view.
    // e.g. self.myOutlet = nil;
}



-(void)setUserCenterView//设置个人中心的界面
{
    //zhouhao de daima
    if([LoginStatus sharedLoginStatus].ifLogined)
    {
//        LoginViewController *login = [[LoginViewController alloc] init];
        userImage.image = [UIImage imageNamed:@"userImage"];
//        NSLog(@"%@",login.userName);
//        label1.text = login.userName;
//        label2.text = login.address;
        loginButton.enabled = NO;
        loginButton.alpha = 0;
        registerButton.enabled = NO;
        registerButton.alpha = 0;
        logoutButton.enabled = YES;
        logoutButton.alpha = 1.0f;
        courseButton.backgroundColor=[UIColor colorWithPatternImage:[UIImage imageNamed:@"UserCenter_ButtonBakcGround_Green_ImageView"]];
        recommendButton.backgroundColor=[UIColor colorWithPatternImage:[UIImage imageNamed:@"UserCenter_ButtonBakcGround_Green_ImageView"]];
        settingButton.backgroundColor=[UIColor colorWithPatternImage:[UIImage imageNamed:@"UserCenter_ButtonBakcGround_Green_ImageView"]];
        [courseButton setTitleColor:[UIColor greenColor] forState:UIControlStateNormal];
        [recommendButton setTitleColor:[UIColor greenColor] forState:UIControlStateNormal];
        [settingButton setTitleColor:[UIColor greenColor] forState:UIControlStateNormal];
    }else {
        userImage.image = [UIImage imageNamed:@"UserCenter_UserPhoto_Empty_ImageView"];
        label1.text=@"您还未登陆";
        label2.text=@"登陆享受更多精彩内容";
        loginButton.enabled = YES;
        loginButton.alpha = 1.0f;
        registerButton.enabled = YES;
        registerButton.alpha = 1.0f;
        logoutButton.enabled = NO;
        logoutButton.alpha = 0;
        courseButton.backgroundColor=[UIColor colorWithPatternImage:[UIImage imageNamed:@"UserCenter_ButtonBakcGround_Gray_ImageView"]];
        [courseButton setTitleColor:[UIColor grayColor] forState:UIControlStateNormal];
        recommendButton.backgroundColor=[UIColor colorWithPatternImage:[UIImage imageNamed:@"UserCenter_ButtonBakcGround_Gray_ImageView"]];
        [recommendButton setTitleColor:[UIColor grayColor] forState:UIControlStateNormal];
        settingButton.backgroundColor=[UIColor colorWithPatternImage:[UIImage imageNamed:@"UserCenter_ButtonBakcGround_Gray_ImageView"]];
        [settingButton setTitleColor:[UIColor grayColor] forState:UIControlStateNormal];
    }
}

- (IBAction)userCenterButtonPressed:(id)sender {
    UIButton *btn = sender;
    int tag = btn.tag;
    if ([LoginStatus sharedLoginStatus].ifLogined) {
        if (tag == 101) {
            UserCourseViewController *usercourseView=[[UserCourseViewController alloc]init];
            [self.navigationController pushViewController:usercourseView animated:YES];
        }else if (tag == 102) {
            CourseRecommendViewController *courseRecomView=[[CourseRecommendViewController alloc]init];
            
            [self.navigationController pushViewController:courseRecomView animated:YES];
        }else if (tag == 103) {
            SettingsViewController *settingsView=[[SettingsViewController alloc]init];
            
            [self.navigationController pushViewController:settingsView animated:YES];
        }
        
    }else{
        [self loginButtonPressed];//未登陆则跳到登陆界面
    }
}

-(IBAction)loginButtonPressed //登陆按钮被点击
{
    LoginViewController *loginView = [[[LoginViewController alloc] init] autorelease];
    [self presentViewController:loginView animated:YES completion:nil];
}
-(IBAction)registerButtonPressed//注册按钮被点击
{
    RegisterViewController *registerView =[[[RegisterViewController alloc]init]autorelease];
    [self presentViewController:registerView animated:YES completion:nil];
}
- (IBAction)logoutUser:(id)sender {
    [LoginStatus sharedLoginStatus].ifLogined=NO;
    [self.navigationController popViewControllerAnimated:YES];
}

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
    return (interfaceOrientation == UIInterfaceOrientationPortrait);
}

#pragma mark UploadPhoto Function

- (void) clickCamera
{
    NSLog(@"aaa");
    if ([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera])
    {
        UIActionSheet *actionSheet;
        actionSheet = [[UIActionSheet alloc] initWithTitle:@"" delegate:self cancelButtonTitle:NSLocalizedString(@"Cancel",@"") destructiveButtonTitle:nil otherButtonTitles:NSLocalizedString(@"Take Photo",@"+US give US to Translate"), NSLocalizedString(@"Photo Gallery",@""), nil];
        [actionSheet showInView:self.view];
        
    }
    else 
    {
        [self showModalImagePicker:UIImagePickerControllerSourceTypePhotoLibrary];
    }
    
}

- (void) showModalImagePicker :(UIImagePickerControllerSourceType) sourceType
{
    UIImagePickerController *imagePicker =  [[UIImagePickerController alloc] init];
    imagePicker.navigationBar.barStyle = UIBarStyleBlack;
    imagePicker.navigationBar.tintColor = [UIColor blackColor];
    
    imagePicker.sourceType = sourceType;
    imagePicker.delegate = self;
    
    [self.navigationController presentModalViewController:imagePicker animated:YES];
}


- (void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex
{
    if (buttonIndex == 0)
    {
        [self showModalImagePicker:UIImagePickerControllerSourceTypeCamera];
    }
    else if (buttonIndex == 1)
    {
        [self showModalImagePicker:UIImagePickerControllerSourceTypePhotoLibrary];
    }
}

- (void)imagePickerControllerDidCancel:(UIImagePickerController *)picker
{
    [picker dismissModalViewControllerAnimated:YES];
}

- (void)imagePickerController:(UIImagePickerController *)picker 
        didFinishPickingImage:(UIImage *)image
                  editingInfo:(NSDictionary *)editingInfo
{
    [picker dismissModalViewControllerAnimated:YES];
    
}

- (void)dealloc {
    [courseButton release];
    [recommendButton release];
    [settingButton release];
    [label2 release];
    [label1 release];
    [userImage release];
    [loginButton release];
    [registerButton release];
    [logoutButton release];
    [super dealloc];
}

@end
